var playerLoop = false;
var playerDisplay = null;
var playerRandom = false;
var playerSort = '';
var playerSortDesc = false;
var maxSong = 99999999;
var currentSong = 0;
var playerList = -1;
var playerDiv = null;
var playerTag = null;

function playerAction(action)
{
	var element;
	var nextSong;
	var select=document.getElementById("playerlist");
	var selectIndex = select.selectedIndex;
	var selectOptions = select.options;
	
	if (action == 'next song')
	{
		if (selectOptions[selectIndex].value == 'list')
		{
			if (playerRandom)
			{
				nextSong = Math.floor(Math.random()*maxSong);
			}
			else
			{
				nextSong = currentSong + 1;
			}
		}
		else
		{
			nextSong = currentSong;
		}
		setSong(nextSong, playerList, playerSort, playerSortDesc);
	}
	else if (action == 'previous song')
	{
		if (selectOptions[selectIndex].value == 'list')
		{
			if (playerRandom)
			{
				nextSong = Math.floor(Math.random()*maxSong);
			}
			else
			{
				nextSong = currentSong - 1;
			}
		}
		else
		{
			nextSong = currentSong;
		}
		setSong(nextSong, playerList, playerSort, playerSortDesc);
	}
	else if (action == 'shuffle')
	{
		element = $('#playshuffle');
		element.toggleClass('selected');
		playerRandom = element.hasClass('selected');

	}
	else if (action == 'loop')
	{
		element = $('#playloop');
		element.toggleClass('selected');
		playerLoop = element.hasClass('selected');
	}
}

function setupPlayer()
{
	playerDisplay = $('#nowplaying');
	playerDiv = $('#audioTag');
	$('#player').draggable({containment: 'window'});
	$('#player').draggable('option', 'cancel', 'audio, a, select');
}

function setSong(songNumber, list, sort, desc)
{
	playerShow(true);

	if (songNumber < 0)
	{
		songNumber = maxSong-1;
	}
	if (songNumber >= maxSong)
	{
		songNumber = 0;
	}
	playerSort = sort;
	playerSortDesc = desc;
	playerList = list;
	$.ajax({
		  type: 'POST',
		  url: "/downloadfile",
		  data: "index="+songNumber+"&list="+list+"&desc="+desc+"&sort="+sort,
		  success: getSongCallback,
		  dataType: 'json'
		});
	

}

function songEnded()
{
	var select=document.getElementById("playerlist");
	var selectIndex = select.selectedIndex;
	var selectOptions = select.options;
	if (selectOptions[selectIndex].value == 'single')
	{
		if (playerLoop)
		{
			playerAction('next song');
		}
	}
	else
	{
		if (playerRandom)
		{
			playerAction('next song');
		}
		else
		{
			if (playerLoop)
			{
				playerAction('next song');
			}
			if (currentSong+1 < maxSong)
			{
				playerAction('next song');
			}
		}
	}
}

function getSongCallback(data)
{
	if (data == null || data.key == null)
		return;
	playerDisplay.html('Playing: '+data.title);
	maxSong = data.count;
	currentSong = data.index;
	playerDiv.html('');
	playerDiv.html("<audio onended='songEnded()' controls='controls'><source id='audioSource' src='/downloadfile?key="+data.key+"' ></source>Your browser does not support the audio element.</audio>");
	playerTag = playerDiv.find('audio');
	playerTag[0].play();
}